package com.wnxy.pms.order.core.controller;


import com.wnxy.pms.common.api.Result;
import com.wnxy.pms.common.api.enums.impl.ResultCode;
import com.wnxy.pms.order.core.dto.OrderDto;
import com.wnxy.pms.order.core.exception.BusinessException;
import com.wnxy.pms.order.core.service.IOrderService;
import com.wnxy.pms.order.core.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Jet
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/auth/order")
@Api(tags = "订单模块")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping
    @ApiOperation("创建订单")
    public Result createOrder(@RequestBody OrderDto orderDto){
        // 创建订单，返回订单编号
        String orderSn = orderService.createOrder(orderDto);
        return Result.ok(orderSn);
    }

    /**
     * 根据订单编号查询订单接口
     */
    @GetMapping("/{orderSn}")
    @ApiOperation("根据订单编号查询订单信息")
    public Result<OrderVo> getOrderBySn(
            @PathVariable("orderSn") @ApiParam(name="orderSn",value = "订单编号") String orderSn){
        OrderVo orderVo = orderService.findByOrderSn(orderSn);
        return Result.ok(orderVo);
    }

    @PutMapping("/{orderSn}")
    @ApiOperation("更新订单状态")
    public Result update(@PathVariable("orderSn") String orderSn){

        Integer status = null;
        try {
            status = orderService.updateByOrderSn(orderSn);
        } catch (BusinessException e) {
            return Result.fail(ResultCode.FAILED.getCode(),e.getMessage());
        }
        return Result.ok(status);
    }
}
